import java.util.Arrays;

public class MyArrayList implements IList{
    public int[] elem;
    public int usedSize;

    public static final int DEFAULT_CAPACITY = 5;

    public MyArrayList() {
        elem = new int[DEFAULT_CAPACITY];
    }

    public MyArrayList(int initCapacity) {
        elem = new int[initCapacity];
    }

    /**
     * 把数据data存放到顺序表当中  默认是放到顺序表最后一个位置
     * @param data
     */
    @Override
    public void add(int data) {
        if(isFull()) {
            //扩容 -》 2倍扩容
            grow();
        }
        elem[usedSize++] = data;
    }

    private void grow() {
        elem = Arrays.copyOf(elem,2*elem.length);
    }

    private void grow(int growNum) {
        elem = Arrays.copyOf(elem,growNum*elem.length);
    }

    public boolean isFull() {
        return usedSize == elem.length;
    }


    @Override
    public void add(int pos, int data) {
        checkPos(pos);
        if(isFull()) {
            grow();
        }
        //1.移动元素
        for (int i = usedSize-1; i >= pos ; i--) {
            elem[i+1] = elem[i];
        }
        elem[pos] = data;
        usedSize++;
    }

    private void checkPos(int pos) {
        if(pos < 0 || pos > usedSize) {
            throw new PosOutOfBoundsException("添加元素时pos位置不合法："+pos);
        }
    }



    @Override
    public boolean contains(int toFind) {
        for (int i = 0; i < usedSize; i++) {
            if(elem[i] == toFind) {
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        for (int i = 0; i < usedSize; i++) {
            if(elem[i] == toFind) {
                return i;
            }
        }
        return -1;
    }

    @Override
    public int get(int pos) {
        checkPosByGetOrSet(pos);
        if(isEmpty()) {
            throw new ListEmptyException("获取元素时，顺序表为空...");
        }
        return elem[pos];
    }

    private void checkPosByGetOrSet(int pos) {
        if(pos < 0 || pos >= usedSize) {
            throw new PosOutOfBoundsException("获取元素时pos位置不合法："+pos);
        }
    }

    public boolean isEmpty(){
        return usedSize == 0;
    }

    @Override
    public void set(int pos, int value) {
        checkPosByGetOrSet(pos);

        elem[pos] = value;
    }

    @Override
    public void remove(int toRemove) {
        int index = indexOf(toRemove);
        if(index == -1) {
            System.out.println("没有你要删除的数据...");
            return;
        }

        for (int i = index; i < usedSize-1; i++) {
            elem[i] = elem[i+1];
        }
        //引用类型
        //elem[i] = null;
        usedSize--;
    }

    @Override
    public int size() {
        return usedSize;
    }

    @Override
    public void clear() {
        /*for (int i = 0; i < usedSize; i++) {
            elem[i] = null;
        }*/
        usedSize = 0;
    }

    @Override
    public void display() {
        for (int i = 0; i < usedSize; i++) {
            System.out.print(elem[i] +" ");
        }
        System.out.println();
    }
}
